const jwt  = require('jsonwebtoken');
// 秘钥
let secret = '1q2w3e4r5t.';
//用户token 加密
function BuildToken(userInfo){
  let token = jwt.sign(userInfo,secret,{expiresIn:60*60*6}); // 授权时效6小时
  return token;
}
//解析token
function ParseToken(token){
  let userInfo = jwt.verify(token,secret,(err,scoded)=>{
    return err?false:scoded
  })
  //解析token是否成功
  if(!userInfo) return false 
  let nowtime = Math.ceil((new Date()).getTime()/1000) + 300
  const {username,password,iat,exp} = userInfo;
  // 验证token 是否过期
  if(nowtime>exp||nowtime<iat){
    return false
  }else{
    return {username,password};
  }
}
module.exports = {
  BuildToken,
  ParseToken,
}